Using ZK JndiVariableResolver - Retrieve Session Beans and EntityManagerFactory
Jeff Liu, Engineer, Potix Corporation
November 29, 2007
Applicable to ZK 3.0.1 Freshly (zk-3.0.1-FL-2007-12-07 and later)
- Applicable to JBoss AS 4.0.5.GA
Introduction
Java Platform, Enterprise Edition (Java EE) is the industry standard for developing portable, robust, scalable and secure server-side Java applications. Enterprise JavaBeans (EJB) technology is the server-side component architecture for Java Platform, Enterprise Edition (Java EE). EJB technology enables rapid and simplified development of distributed, transactional, secure and portable applications based on Java technology.
By bundling ZK into a JavaEE application server (JBoss is used in this tutorial) and using ZK JndiVariableResolver, ZK developers are now able to combine EJB into ZK application easily.
If you are not familiar with EJB, here is a comprehensive EJB tutorial http://www.laliluna.de/ejb-3-tutorial-jboss.html
Retrieving Session Beans and EntityManagerFactory from ZK
- Following I am going to present you an example on how to use ZK JndiVariableResolver to retrieve session beans and entityManagerFactory
Add JndiVariabelResolver to Page
- Using ZK JndiVariableResolver, session beans and entityManagerFactory can be retrieved by JNDI binding. First, Declare the directive
- Using Default Constructor
<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
<window>
...
</window>
- Also, JndiVariableResolver constructor can take two extra agruments for special JNDI patterns.
- prepend - The prepended part of JNDI name Ex: "prepend/ejbBean/local"
- mapping - The key-value pairs for JNDI name and its corresponding variable name Ex:"a=custom/MySession,b=custom/MySession2,emf=java:/EntityManagerFactory"
/*
arg0 is first argument of variable-resolver constructor
arg1 is second argument of variable-resolver constructor
*/
<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" agr0="ZkEJB3Demo"
arg1="mail=java:comp/env/mailing,sec=java:comp/security/module" ?>
<window>
...
</window>
- JndiVariableResolver will resolve variable in following order:
- java:comp/env
- java:comp
- java:
- The variable will be look up as a session beans with prepend.
- The key-value pairs which is defined by mapping
Retrieve session beans
- Since the session bean is bound to the java:comp/env by the configuration in jboss-web.xml and web.xml, the session bean can be easily retrieved by using JndiVariableResolver.
- Declaring an EJB reference in jboss-web.xml and web.xml
jboss-web.xml
<ejb-local-ref>
<ejb-ref-name>personLocalBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>demo.PersonBeanLocal</local>
<local-jndi-name>ZkEJB3Demo/PersonBean/local</local-jndi-name>
</ejb-local-ref>
web.xml
<ejb-local-ref>
<ejb-ref-name>personLocalBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>demo.PersonBeanLocal</local-home>
<local>demo.PersonBeanLocal</local>
</ejb-local-ref>
person.zul
<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
<window>
<zscript>
//Use session bean simply by its name "personLocalBean"
personLocalBean.createDemoData();
List t = personLocalBean.getAllPersons();
</zscript>
<listbox width="600px">
<listhead sizable="true">
<listheader label="name" sort="auto"/>
<listheader label="email" sort="auto"/>
</listhead>
<listitem forEach="${t}">
<listcell label="${each.name}"/>
<listcell label="${each.email}"/>
</listitem>
</listbox>
</window>
Snapshot:
Retrieve EntityManagerFactory
- Persistence units are not bound into JNDI by default, we define JBoss specific properties in persistence.xml to bind them into JNDI. As a result, we are able to retrieve the entity manager factory by JndiVariableResolver
persistence.xml
...
<properties>
<property name="jboss.entity.manager.factory.jndi.name" value="java:comp/entityManagerFactory"/>
</properties>
</persistence-unit>
...
person2.zul
<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
<window>
<zscript>
import javax.persistence.EntityManager;
//Use EntityManagerFactory simply by its name "entityManagerFactory"
EntityManager em = entityManagerFactory.createEntityManager();
List persons= em.createQuery("from Person").getResultList();
em.close();
</zscript>
<listbox width="600px">
<listhead sizable="true">
<listheader label="name" sort="auto"/>
<listheader label="email" sort="auto"/>
</listhead>
<listitem forEach="${persons}">
<listcell label="${each.name}"/>
<listcell label="${each.email}"/>
</listitem>
</listbox>
</window>
Bundle ZK into JBoss by Eclipse
Step by Step
- Create a Dynamic Web Project and bundle ZK to it, refer to ZK Quick Start Guide
- Create an EJB3 Project
- Create a JavaEE- Enterprise Application Project and setup the module dependencies for the dynamic web project and EJB3 project in previous steps
After the steps above, the Deployment Descriptor(application.xml) should be look like this:
<?xml version="1.0" encoding="UTF-8"?>
<application id="Application_ID" version="1.4" ...>
<display-name>
ZkEJB3Demo</display-name>
<module>
<web>
<web-uri>DemoWeb.war</web-uri>
<context-root>DemoWeb</context-root>
</web>
</module>
<module >
<ejb>DemoEJB3.jar</ejb>
</module>
</application>
Download
- ZkEJB3Demo.ear - Deployable Enterprise Archive
- DemoEJB3.zip - DemoEJB3 Eclipse Project Sourcecode
- DemoWeb.zip - DemoWeb Eclipse Project Sourcecode
- ZkEJB3Demo.zip - ZkEJB3Demo Eclipse Project Sourcecode
Deploy ZkEJB3Demo.ear
- Notice: before deploy the demo project, please make sure that the JBoss AS datasource is setup correctly. Refer to http://wiki.jboss.org/wiki/Wiki.jsp?page=ConfigDataSources
- Stop JBoss Application Server
- Copy ZkEJB3Demo.ear to JBOSS_HOME\server\default\deploy\
- Start JBoss Application Server
Conclusion
- ZK developres should have a general idea of ZK JndiVariableResolver now. Using JndiVariableResolver, ZK developers can retrieve any JNDI env variables, not only EJB3 variables. For example: If you have a mailing java object which is bound to java:comp/env/mail, ZK developers can retrieve it easily by JndiVariableResolver. If you have futher question, feel free to leave comment here.
Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License. |